package soa.security.web.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import soa.security.Constants;
import soa.security.domain.Department;
import soa.security.domain.LdapTree;
import soa.security.domain.Organization;
import soa.security.domain.Person;
import soa.security.domain.Position;
import soa.security.web.export.LdapTreeJsonFormatExport;
import soa.security.web.support.action.LdapModelAction;
import v1.web.WebUtils;

/**
 * 
 * @author hueb
 *
 *         2015年4月22日
 */
public class PositionAction extends LdapModelAction<Position> {

	private static final long serialVersionUID = -3352868352142131382L;

	private String positionId;

	public String grid() {
		if (StringUtils.isBlank(organizationId))
			return SUCCESS;

		if (StringUtils.isNotBlank(departmentId)) {
			List<Position> all = positionMgrService.allPositions(
					organizationId, buildContext().getParameters(), subTree);

			updateResultContent(positionMgrService.allPositions(all,
					departmentMgrService.allPositions(organizationId,
							departmentId)));
		} else
			updateResultContent(positionMgrService.allPositions(organizationId,
					buildContext().getParameters(), subTree));

		return SUCCESS;
	}

	public String edit() {
		Position position = getModel();
		if (StringUtils.isBlank(position.getBid())) {
			// add
			position.setBid("PO"
					+ positionMgrService.findMaxNumber(Position.class));

		}

		Organization or = position.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);
		position.setOrganization(or);

		updateResultContent(position);
		orId = position.getOrganization().getBid();
		return SUCCESS;
	}

	public String view() {
		Position position = getModel();
		Organization or = position.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);
		position.setOrganization(or);

		orId = position.getOrganization().getBid();
		return SUCCESS;
	}

	public String doneSave() {
		positionMgrService.addPosition4Mgr(organizationId, getModel(),
				departmentDns, personDns);
		return SUCCESS;
	}

	public String doneUpdate() {
		positionMgrService.updatePosition4Mgr(organizationId, getModel(),
				departmentDns, personDns);

		return SUCCESS;
	}

	/**
	 * 指定机构下的部门树，需要checked
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */
	public String deptInOneOrgCheckTree() {
		List<Department> checked = positionMgrService.findDepartments(
				organizationId, Constants.POSITIONS, dnp);
		List<LdapTree> trees = treeMgrService.deptInOneOrgCheckTree(
				organizationId, checked, departmentId);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	/**
	 * 指定机构下的人员树，需要checked
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */
	public String personInOneOrgCheckTree() {
		List<Person> checked = positionMgrService.allPersons(organizationId,
				getModel().getBid());
		List<LdapTree> trees = treeMgrService.personInOneOrgCheckTree(
				organizationId, checked);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	@Override
	public String remove() {
		positionMgrService.remove(organizationId, dnp);
		return SUCCESS;
	}

	@Override
	protected Position findByDn(String dn) {
		return positionMgrService.findByDn(dn);
	}

	public String getPositionId() {
		return positionId;
	}

	public void setPositionId(String positionId) {
		this.positionId = positionId;
	}
}
